package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreCompensation;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.CompensationForm;
import com.edu.hre.struts.form.admin.CompensationListForm;

public class CompensationDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "compensationDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public ArrayList<CompensationForm> getCompensationListInfo(int limit, int offset){

		List<HreCompensation> hreCompensations = baseService.getPojos(null, -1, null, new String[]{"year", "month"}, new Boolean[]{true, true}, limit, offset, HreCompensation.class);
		ArrayList<CompensationForm> compensations = new ArrayList<CompensationForm>();
		if (hreCompensations == null) {
			return compensations;
		}
		for (HreCompensation hreCom: hreCompensations) {
			
			try {

				CompensationForm compensation = new CompensationForm();
				compensation.setId(hreCom.getId());
				compensation.setYear(hreCom.getYear() + "");
				compensation.setMonth(StringConstants.ALL_MONTHS[hreCom.getMonth()-1]);
				compensation.setAmount(String.format("%,15.2f", hreCom.getAmount()) + " VND");
				
				compensations.add(compensation);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return compensations;
	}

	public int countCompensations(){

		int count = baseService.countByColumns(null, null, null, "id", HreCompensation.class);
		return count < 0 ? 0 : count;
	}

	public void deleteSkills(CompensationListForm form){

		ArrayList<CompensationForm> info = (ArrayList<CompensationForm>) form.getAllCompensations();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				CompensationForm com = info.get(i);
				if (com.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, com.getId(), null, null, 0, HreCompensation.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
